<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="core.*,util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="/WEB-INF/include/checkAdmin.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    form > div:not(.box) { margin-top: 20px; }
    div.box label { display: inline-block; margin-right: 20px; }
    body table.table { width: 400px; }
    tbody tr:hover { background-color: #FFC; cursor: pointer; }
    textarea { width: 700px; height: 100px; margin-bottom: 0px; }
</style>
<script>
$(function() {
    $("tr").click(function() {
        $("input[type=checkbox]", this).trigger("click");
    })
})
</script>
</head>
<%
    String errMsg = null,infoMsg = null;

    RequestParameter param = new RequestParameter(request);
    String cmd = param.getString("cmd", "");
    int id = param.getInt("id", 0);
    if (id == 0) throw new Exception("파라미터 오류");
    UserGroup group = UserGroupDAO.selectById(id);

    String name = param.getString("name", group.getName());
    String category = param.getString("category", group.getCategory());

    if (request.getMethod().equals("POST")) {
        if (id > 0) {
            if ("저장".equals(cmd)) {
                if (name.length() >= 2) {
                    if (UserGroupDAO.selectByName(name) == null) {
                        UserGroupDAO.update(id, name, category);
                        infoMsg = "저장되었습니다";
                    }
                    else errMsg = "그룹명이 중복됩니다";
                }
                else errMsg = "그룹명이 너무 짧습니다";
            } else if ("그룹삭제".equals(cmd)) {
                UserGroupDAO.deleteUserGroup(id);
                response.sendRedirect("usergroupList.jsp");
                return;
            } else if ("사용자제거".equals(cmd)) {
                int[] userIDList = param.getIntValues("userID");
                UserGroupDAO.removeUser(id, userIDList);
            } else if ("사용자추가".equals(cmd)) {
                String[] loginIDList = param.getString("loginID", "").split("[ ,\t\n]+");
                UserGroupDAO.addUser(id, loginIDList);
            }
        }
        else errMsg = "잘못된 접근입니다";
    }

    ArrayList<User> userList = UserDAO.selectByUserGroupId(id);
%>
<body>
<%@ include file="/WEB-INF/include/menu.jsp" %>

<div class="container main">

    <h1>사용자 그룹</h1>
    <hr />

    <form method="post">
        <input type="hidden" name="id" value="<%= id %>" />

        <div>그룹명</div>
        <input type="text" name="name" value="<%= name %>" />

        <div>범주</div>
        <input type="text" name="category" value="<%= category %>" />

        <div>
            <button type="submit" class="btn" name="cmd" value="저장">
                <i class="icon-hdd"></i> 저장
            </button>
            <button type="submit" class="btn" name="cmd" value="그룹삭제"
                    onclick="return confirm('사용자그룹을 삭제하시겠습니까?');">
                <i class="icon-remove"></i> 사용자그룹 삭제
            </button>
            <a href="usergroupList.jsp" class="btn"><i class="icon-list"></i> 목록으로</a>
        </div>

        <hr />

        <table class="table table-bordered table-condensed">
            <thead><tr><th>선택</th><th>로그인ID</th><th>이름</th></tr></thead>
            <tbody>
            <% for (User user : userList) { %>
                <tr>
                    <td><input type="checkbox" name="userID" value="<%=user.getId()%>" /></td>
                    <td><%=user.getLoginId()%></td>
                    <td><%= user.getName() %></td>
                </tr>
            <% } %>
            </tbody>
        </table>

        <div>
            <button type="submit" class="btn" name="cmd" value="사용자제거">
                <i class="icon-remove"></i> 선택된 사용자 그룹에서 제거
            </button>
        </div>

        <hr />

        <div>로그인ID 목록을 입력하고 추가 버튼을 누르세요</div>
        <textarea name="loginID"></textarea>

        <div>
            <button type="submit" class="btn" name="cmd" value="사용자추가">
                <i class="icon-plus"></i> 그룹에 사용자 추가
            </button>
        </div>

    </form>

    <% if (errMsg != null) { %>
        <div class="alert alert-error"><%= errMsg %></div>
    <% } %>
    <% if (infoMsg != null) { %>
        <div class="alert alert-info"><%= infoMsg %></div>
    <% } %>

</div>

<%@ include file="/WEB-INF/include/footer.jsp" %>
</body>
</html>
